<?php
namespace app\notify\controller;
use think\Db;
use think\Exception;

class Notify extends Base
{
    public function Index(){
        $data = $this->request->post();
        $host =$this->request->host();
        if ($host!=config('myself_notify_url')){
            return $this->sendFail($host."不正确");
        }
        try{
            if ("payment" ==$data["notify_type"]){
                $this->payment($data);
            }else{
                $this->payNotify($data);
            }
        }catch (Exception $e){
            return $this->sendFail("异常：".$e->getMessage());
        }
        return $this->sendFail($host."no");
    }

    /**
     * 充值情况回调
     */
    private function payNotify($data){
        $where = array();
        $where["transaction_no"] =$data["transaction_no"];
        $where["wallet_id"]=$data["wallet_id"];
        $where["coin_symbol"]=$data["coin_symbol"];
        $where["to_address"]=$data["amount"];
        $amount=$data["amount"];
        $log = Db::table("tb_withdrawcoin_log")->where($where)->find();
        if (!$log){
            return $this->sendFail("转账不存在w=:".json_encode($where));
        }
        $notify_type= $data["notify_type"];
        //充值成功
        $updateData = array();
        $updateData["mark"]=json_encode($data);
        $isPayOk = $notify_type=="confirm";
        if ($isPayOk){
            $updateData["status"]=2;
            $updateData["txid"]=$data['tx_id'];
        }else{
            //充值失败
            $updateData["status"]=3;
        }
        $id = $log["id"];
        $uid= $log["uid"];
        Db::startTrans();
        $updateDataRes = Db::table("tb_withdrawcoin_log")->where("id",$id)->update($updateData);
        if (!$updateDataRes){
            Db::rollback();
            return $this->sendFail("更新订单状态失败");
        }

        //解冻
        $froze= Db::table("tb_wallet")->where("wallet_id",$data['wallet_id'])->where("coin_symbol",$data['coin_symbol'])->setDec("froze",abs($amount));
        if (!$froze){
            Db::rollback();
            return $this->sendFail("解除冻结余额失败");
        }
        moneyRecord($uid,abs($amount),$isPayOk?5:6,$isPayOk?"提币成功扣除":"提现失败解冻");
        if (!$isPayOk){
            //增加解除冻结余额
            $number= Db::table("tb_wallet")->where("wallet_id",$data['wallet_id'])->where("coin_symbol",$data['coin_symbol'])->setDec("froze",abs($amount));
            if (!$number){
                Db::rollback();
                return $this->sendFail("解冻增加资金失败");
            }
        }
        Db::commit();
        return $this->sendSuccess("回调成功");
    }

    /**
     * @param $data
     * 充值
     */
    private function payment($data){
        /**
         * 充值参数 $to_address 地址  $coin_symbol 币种  $amount 数量
         * */
        $from_address = $data["from_address"];
        $to_address = $data["to_address"];
        $coin_symbol = $data["coin_symbol"];
        $amount = $data["amount"];

        /**
         * 币种增加 数量
         */
        Db::startTrans();
        $userWallet = Db::table('wallet')->where('coin_symbol', $coin_symbol)->where('address', $to_address)->find();
        $status = 0;
        if ($userWallet){
            $inc = Db::table('wallet')->where('coin_symbol', $coin_symbol)->where('address', $to_address)->setInc('number', $amount);
        }
        if ($inc){
            $status =1;
            $data['uid'] = $userWallet["uid"];
        }else{
            $data['uid'] = 0;
        }
        $data['create_time'] = time();
        $data['status'] = $status;

        /**
         * 充值日志记录
         */
        $insertLog = Db::table('wallet_log')->insert($data);
        if ($insertLog){
            Db::commit();
            return $this->sendSuccess('操作成功');
        }else{
            Db::rollback();
            return $this->sendFail('操作失败');
        }

    }


}